//
//  SDFile.m
//  SKinDiary
//
//  Created by pcyang on 16/6/26.
//  Copyright © 2016年 tencent. All rights reserved.
//

#import "SDFile.h"

@implementation SDFile

+ (NSDate*)getItemCreatedTime:(NSString*)path
{
    NSFileManager* defaultManager = [NSFileManager defaultManager];
    BOOL pathIsDirectory;
    if ([defaultManager fileExistsAtPath:path isDirectory:&pathIsDirectory] && !pathIsDirectory)
        return [[defaultManager attributesOfItemAtPath:path error:nil] fileCreationDate];
    else
        return [NSDate distantFuture];
}

+ (NSDate*)getItemModifiedTime:(NSString*)path
{
    NSFileManager* defaultManager = [NSFileManager defaultManager];
    BOOL pathIsDirectory;
    if ([defaultManager fileExistsAtPath:path isDirectory:&pathIsDirectory] && !pathIsDirectory)
        return [[defaultManager attributesOfItemAtPath:path error:nil] fileModificationDate];
    else
        return [NSDate distantFuture];
}

+ (void)setItemModifed:(NSString*)path
{
    NSFileManager* defaultManager = [NSFileManager defaultManager];
    BOOL pathIsDirectory;
    if ([defaultManager fileExistsAtPath:path isDirectory:&pathIsDirectory] && !pathIsDirectory)
        assert([defaultManager setAttributes:[NSDictionary dictionaryWithObject:[NSDate date] forKey:NSFileModificationDate] ofItemAtPath:path error:nil]);
}




+ (unsigned long long)getFileSize:(NSString*)path
{
    NSFileManager* defaultManager = [NSFileManager defaultManager];
    BOOL pathIsDirectory ;
    if ([defaultManager fileExistsAtPath:path isDirectory:&pathIsDirectory] && !pathIsDirectory)
        return [[defaultManager attributesOfItemAtPath:path error:nil] fileSize];
    else
        return 0;
}



+ (BOOL)fileExist:(NSString*)path
{
    NSFileManager* defaultManager = [NSFileManager defaultManager];
//    BOOL pathIsDirectory;
//    if ([defaultManager fileExistsAtPath:path isDirectory:&pathIsDirectory] && !pathIsDirectory)
//        return TRUE;
//    
//    else
//        return  FALSE;
    if ([defaultManager fileExistsAtPath:path]) {
        return true;
    }
    
    return false;
    
}
+ (NSString*)pathForMainBundleResource:(NSString*)fileName
{
    NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
    return [resourcePath stringByAppendingPathComponent:fileName];
}
+ (BOOL)deleteItem:(NSString*)path
{
    NSFileManager* defaultManager = [NSFileManager defaultManager];
    return [defaultManager removeItemAtPath:path error:nil];
}

+ (void)moveFileFrom:(NSString*)fromPath to:(NSString*)toPath
{
    if ([fromPath isEqualToString:toPath])
        return;
    
    NSFileManager* defaultManager = [NSFileManager defaultManager];
    BOOL pathIsDirectory;
    if ([defaultManager fileExistsAtPath:fromPath isDirectory:&pathIsDirectory] && !pathIsDirectory)
    {
        if( [defaultManager fileExistsAtPath:toPath] )
            [defaultManager removeItemAtPath:toPath error:nil];
        [defaultManager moveItemAtPath:fromPath toPath:toPath error:nil];
    }
}

+ (void)copyFileFrom:(NSString*)fromPath to:(NSString*)toPath
{
    if ([fromPath isEqualToString:toPath])
        return;
    
    NSFileManager* defaultManager = [NSFileManager defaultManager];
    BOOL pathIsDirectory;
    if ([defaultManager fileExistsAtPath:fromPath isDirectory:&pathIsDirectory] && !pathIsDirectory)
    {
        if( [defaultManager fileExistsAtPath:toPath] )
            [defaultManager removeItemAtPath:toPath error:nil];
        [defaultManager copyItemAtPath:fromPath toPath:toPath error:nil];
    }
}

+ (void)deleteDictionary:(NSString*)direString
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *tempArray = [fileManager contentsOfDirectoryAtPath:direString error:nil];
    for (NSString *fileName in tempArray)
    {
        NSString *fullPath = [direString stringByAppendingPathComponent:fileName];
        [SDFile deleteItem:fullPath];
    }
}

+ (long long)folderSizeAtPath:(NSString*) folderPath
{
    NSFileManager* manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:folderPath]) return 0;
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
    NSString* fileName;
    long long folderSize = 0;
    while ((fileName = [childFilesEnumerator nextObject]) != nil){
        NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
        folderSize += [SDFile getFileSize:fileAbsolutePath];
    }
    return folderSize;
}


+ (BOOL) appendString:(NSString*) string toFileWithFilePath:(NSString*) path
{
    BOOL isSuccess = NO;
    BOOL isExit = [[NSFileManager defaultManager] fileExistsAtPath:path];
    
    if (NO==isExit)
    {
        isSuccess = [string writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
    }
    else
    {
        NSData* content = [string dataUsingEncoding:NSUTF8StringEncoding];
        if (content)
        {
            NSFileHandle *outFile = [NSFileHandle fileHandleForWritingAtPath:path];
            if (outFile)
            {
                [outFile seekToEndOfFile];
                [outFile writeData:content];
                isSuccess = YES;
                
                [outFile closeFile];
            }
        }
    }
    
    return isSuccess;
}

@end

@implementation SDFile(Directory)

+ (NSString*)getDocumentDirectory;
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* path = [paths objectAtIndex:0];
    
    /*path = [path stringByAppendingFormat:@"/%@",versionId];
     
     BOOL pathIsDirectory;
     NSFileManager* defaultManager = [NSFileManager defaultManager];
     if (FALSE == [defaultManager fileExistsAtPath:path isDirectory:&pathIsDirectory] || !pathIsDirectory)
     [defaultManager createDirectoryAtPath:path withIntermediateDirectories:NO attributes:nil error:nil];*/
    
    return path;
}

+ (NSString*)getCacheDirectory
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains( NSCachesDirectory, NSUserDomainMask, YES);
    NSString* path = [paths objectAtIndex:0];
    return path;
}

+ (NSString*)getDirectoryInDocumentWithName:(NSString*)name
{
    NSFileManager* defaultManager = [NSFileManager defaultManager];
    NSString* docDirectory = [SDFile getDocumentDirectory];
    NSString* newDirectory = [docDirectory stringByAppendingFormat:@"/%@",name];
    
    BOOL pathIsDirectory;
    if (FALSE == [defaultManager fileExistsAtPath:newDirectory isDirectory:&pathIsDirectory] || !pathIsDirectory)
        [defaultManager createDirectoryAtPath:newDirectory withIntermediateDirectories:NO attributes:nil error:nil];
    
    return newDirectory;
}

+ (NSString*)getDirectoryInCacheWithName:(NSString*)name
{
    NSFileManager* defaultManager = [NSFileManager defaultManager];
    NSString* docDirectory = [SDFile getCacheDirectory];
    NSString* newDirectory = [docDirectory stringByAppendingFormat:@"/%@",name];
    
    BOOL pathIsDirectory;
    if (FALSE == [defaultManager fileExistsAtPath:newDirectory isDirectory:&pathIsDirectory] || !pathIsDirectory)
        [defaultManager createDirectoryAtPath:newDirectory withIntermediateDirectories:NO attributes:nil error:nil];
    
    return newDirectory;
}

+ (void)checkDirectoryPath:(NSString*)path
{
    NSFileManager* defaultManager = [NSFileManager defaultManager];
    
    BOOL pathIsDirectory;
    if (FALSE == [defaultManager fileExistsAtPath:path isDirectory:&pathIsDirectory] || !pathIsDirectory)
        [defaultManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
}




@end